package com.vpclub.converter;

import com.vpclub.bean.Employee;
import com.vpclub.dao.DepartmentDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;

/**
 *      2个泛型：
 *         S:Source
 *         T:Target
 *
 * @author : leimingchao
 * @date Date : 2021年08月01日 14:01
 */
public class MyStringToEmployeeConverter implements Converter<String,Employee> {

    @Autowired
    private DepartmentDao departmentDao;

    /**
     * 自定义的转换规则
     * @param source
     * @return
     */
    @Override
    public Employee convert(String source) {
        //employeeAdmin-admin@qq.com-1-101
        System.out.println("页面提交的将要转换的字符串：" + source);
        Employee employee = new Employee();
        if (source.contains("-")) {
            String[] split = source.split("-");
            employee.setLastName(split[0]);
            employee.setEmail(split[1]);
            employee.setGender(Integer.decode(split[2]));
            employee.setDepartment(departmentDao.getDepartment(Integer.decode(split[3])));
        }
        return employee;
    }
}
